package com.hydl.power.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hydl.power.mapper.*;
import com.hydl.power.pojo.*;
import com.hydl.power.service.WebSocketService;
import com.hydl.power.utils.CalculateDistanceUtils;
import com.hydl.power.websocket.WebSocketServer;
import org.apache.commons.lang3.StringUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.websocket.Session;

import java.util.*;

@Service
@EnableScheduling
public class WebSocketServiceImpl implements WebSocketService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private  TemperatureDeviceMapper temperatureDeviceMapper;

    @Autowired
    private  WaterInvasionMapper waterInvasionMapper;

    @Autowired
    private  VoltageMapper voltageMapper;

    @Autowired
    private  TempDeviceMapper tempDeviceMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderDeliveryMapper orderDeliveryMapper;



    //private  final  static Logger logger = LoggerFactory.getLogger(WebSocketServiceImpl. class);

    public static Map<String, String> powerDataMap = new HashMap<>();

    //存放运维人员位置 key:运维id value:经纬度
    public static Map<String, String> locationMap = new HashMap<>();
    public static Map<String, String> locationUserMap = new HashMap<>();

    //电路图多功能表
    public static Map<String, String> powerImgMap = new HashMap<>();

    //电路图电器火灾
    public static Map<String, String> powerImgTemperatureDeviceMap = new HashMap<>();

    public static Map<String, String> projectAlarmStatusMap = new HashMap<>();

    //public static Map<String,Map<String,List<Object>>> temperatureData=new HashMap<>();


    //线缆测温
    public static Map<String,String> temperatureDataMap=new HashMap<>();
    //水浸设备
    public static Map<String,String> waterInvasion=new HashMap<>();

    //电压设备
    public static Map<String,String> voltage=new HashMap<>();

    //线缆测温列表
    public static Map<String,String> temperatureDeviceMap=new HashMap<>();


    //存放客户查看运维人员的位置 key:客户建立连接的名字  value：运维人员的id+项目id;
    public static Map<String, String> customerMap = new HashMap<>();

    //变压器温控数据
    public static Map<String, String> tempDataMap = new HashMap<>();

    //综保数据
    public static Map<String, String> protectDataMap = new HashMap<>();


    @Override
    public void senCarLocation(String message, Session session) {
        JSONObject jsonObject = JSONObject.parseObject(message);
        String username = jsonObject.getString("username");
        List<Distance> list = new ArrayList<>();
        for (Map.Entry entry : locationMap.entrySet()) {
            SysUser user = userMapper.getUserByMaintainerId(Integer.valueOf(entry.getKey().toString()));
            if (user != null) {
                WorkOrder workOrder = workOrderMapper.getWorkOrderByMaintainerId(user.getMaintainerId());
                if (workOrder != null) {
                    Project project = projectMapper.queryProjectByProId(workOrder.getProjectId());
                    Double number = CalculateDistanceUtils.getDistance(entry.getValue().toString(), project.getLongitudeLatitude() + "," + project.getLatitude_latitude(), Ellipsoid.WGS84);
                    Distance distance = new Distance();
                    distance.setSource(entry.getValue().toString());
                    distance.setDistance(number);
                    distance.setTarget(project.getLongitudeLatitude() + "," + project.getLatitude_latitude());
                    distance.setProjectId(project.getProjectId());
                    distance.setName(user.getUsername());
                    list.add(distance);
                } else {
                    Distance distance = new Distance();
                    distance.setSource(entry.getValue().toString());
                    distance.setName(user.getUsername());
                    list.add(distance);
                }
            }
        }
        String s = JSONObject.toJSONString(list);
        String data = "{\"data\":" + s + ",\"method\":\"getCarLocation\"}";
        webSocketServer.sendInfo(username, data);
    }





  /*  @Override
    public void sendLocation(String message) {
        System.out.println(message + "--------------------------------------" + message);
        JSONObject jsonObject = JSONObject.parseObject(message);
        String token = jsonObject.getString("username");
        if (StringUtils.isNotBlank(token)) {
            SysUser user = userMapper.getUserByToken(token);
            if (user != null) {
                if (user.getStatus() == 1) {
                    String location = jsonObject.getString("location");
                    locationMap.put(user.getMaintainerId(), location);

                    List<WorkOrder> list = workOrderMapper.getWorkOrderByUserMaintainerId(user.getMaintainerId());
                    if (list.size() > 0) {
                        int flag = 0;
                        for (WorkOrder orderDelivery : list) {
                            List<SysUser> userList = userMapper.queryUserByProjectIdList(orderDelivery.getProjectId());
                            if (userList.size() > 0) {
                                for (SysUser sysUser : userList) {
                                    for (ConcurrentHashMap.Entry<String, Session> entry : WebSocketServer.sessionPools.entrySet()) {
                                        if (entry.getKey().contains(sysUser.getToken())) {
                                            Project project = projectMapper.queryProjectByProId(orderDelivery.getProjectId());
                                            webSocketServer.sendInfo(entry.getKey(), location + ";" + project.getLongitudeLatitude() + "," + project.getLatitude_latitude());
                                            flag = 1;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (flag == 1) {
                                break;
                            }
                        }

                    }

                }
            }
        }
    }*/


    /**
     * 实时数据
     * @param message
     * @param session
     */
    @Override
    public void senPowerData(String message, Session session) {
        if (StringUtils.isNotBlank(message)) {
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String sn = jsonObject.getString("sn");
            if (sn != null) {
                powerDataMap.put(sid, sn);
                Object powerData = redisTemplate.opsForValue().get(sn);
                //String data="{\"data\":"+powerData+",\"method\":\"getPowerData\"}";
                webSocketServer.sendInfo(sid, JSONObject.toJSONString(powerData));
            } else {
                webSocketServer.sendInfo(sid, "");
            }
        }
    }


    /**
     * 电路图实时数据
     * @param message
     * @param session
     */
    @Override
    public void senPowerImg(String message, Session session) {
        if (StringUtils.isNotBlank(message)) {
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String projectId = jsonObject.getString("projectId");
            String status = jsonObject.getString("status");
            if (status!=null){
                List<TemperatureDevice> temperatureDeviceList = temperatureDeviceMapper.getTemperatureDeviceByProjectId(projectId);
                powerImgTemperatureDeviceMap.put(sid, JSONObject.toJSONString(temperatureDeviceList));
                List list = new ArrayList<>();
                for (TemperatureDevice temperatureDevice : temperatureDeviceList) {
                    Object data = redisTemplate.opsForValue().get(temperatureDevice.getSerialNumber());
                    if (data!=null){
                        list.add(data);
                    }
                }
                webSocketServer.sendInfo(sid, JSONObject.toJSONString(list));
            }else{
                List<Device> deviceList=deviceMapper.getDeviceByProjectId(projectId);
                powerImgMap.put(sid, JSONObject.toJSONString(deviceList));
                List list = new ArrayList<>();
                for (Device device : deviceList) {
                    Object data = redisTemplate.opsForValue().get(device.getSerialNumber());
                    if (data!=null){
                        list.add(data);
                    }

                }
                webSocketServer.sendInfo(sid, JSONObject.toJSONString(list));
            }

        }
    }




  /*  *//**
     * 发送实时温度数据
     *//*
    @Scheduled(cron = "0/10 * * * * ?")
    public void TemperatureData(){
      *//*  DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info(sdf.format(new Date())+"*********每10秒执行一次");*//*
        if (temperatureData.size()!=0) {
            for (Map.Entry entry : temperatureData.entrySet()) {
                List<Object> list=null;
                Map<String, List<Object>> stringListMap = temperatureData.get(entry.getKey().toString());
                for (Map.Entry temperatureDataEntry : stringListMap.entrySet()) {
                    list = stringListMap.get(temperatureDataEntry.getKey().toString());
                    Object data = redisTemplate.opsForValue().get(temperatureDataEntry.getKey().toString());
                    if (list.size()>=6){
                        list.remove(0);
                        list.add(data);
                    }else {
                        list.add(data);
                    }
                    stringListMap.put(temperatureDataEntry.getKey().toString(),list);
                }
                webSocketServer.sendInfo(entry.getKey().toString(),  JSONObject.toJSONString(list));
            }
        }

    }*/




    /**
     * 实时电缆温度
     *
     * @param message
     * @param session
     */
  /*  @Override
    public void sendTemperature(String message, Session session) {
        if (StringUtils.isNotBlank(message)) {
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String sn = jsonObject.getString("sn");
            if (sn != null) {
                Map<String,List<Object>> temperatureDataMap=new HashMap<>();
                Object temperature = redisTemplate.opsForValue().get(sn);
                List<Object> dataList=new ArrayList<>();
                dataList.add(temperature);
                temperatureDataMap.put(sn,dataList);
                temperatureData.put(sid,temperatureDataMap);
                webSocketServer.sendInfo(sid, JSONObject.toJSONString(dataList));
            } else {
                webSocketServer.sendInfo(sid, "");
            }
        }

    }
*/
    /**
     * pc端温度
     */
   /* @Scheduled(cron = "0/6 * * * * ?")
    public void sendTemperatureTemperaturePc(){
        *//*DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info(sdf.format(new Date())+"*********每6秒执行一次");*//*
        if (temperatureMap.size()!=0) {
            for (Map.Entry entry : temperatureMap.entrySet()) {
                Object o = redisTemplate.opsForValue().get(entry.getValue().toString());
                webSocketServer.sendInfo(entry.getKey().toString(), JSONObject.toJSONString(o));
            }
        }
    }*/

   /* public static Map<String,String> temperatureMap=new HashMap<>();

    *//**
     * pc端温度
     *//*
    @Override
    public void sendTemperatureTemperaturePc(String message, Session session) {
        if (StringUtils.isNotBlank(message)) {
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String sn = jsonObject.getString("sn");
            if (sn != null) {
                temperatureMap.put(sid,sn);
                Object temperature = redisTemplate.opsForValue().get(sn);
                webSocketServer.sendInfo(sid, JSONObject.toJSONString(temperature));
            } else {
                webSocketServer.sendInfo(sid, "");
            }
        }
    }
*/


    /**
     * 项目报警 首页坐标跳动
     * @param message
     * @param session
     */
    @Override
    public void sendProjectAlarmStatus(String message, Session session) {
        if (StringUtils.isNotBlank(message)) {
            List<Project> projectList=new ArrayList<>();
            String sid = session.getRequestParameterMap().get("sid").get(0);
            projectAlarmStatusMap.put(sid,sid);
            List<Project> projects = projectMapper.queryProjectAll();
            for (Project project:projects){
                OrderDelivery orderDelivery = orderDeliveryMapper.getAlarmStatusByProjectId(project.getProjectId());
                if (orderDelivery!=null){
                    projectList.add(project);
                }
            }
            String s = JSONObject.toJSONString(projectList);
            webSocketServer.sendInfo(sid, "{\"data\":" + s + ",\"method\":\"projectAlarmStatus\"}");
        }
    }


    /**
     * 运维人员位置
     * @param message
     * @param session
     */
    @Override
    public void addMaintainerLocation(String message, Session session) {
        if (StringUtils.isNotBlank(message)){
            JSONObject jsonObject = JSONObject.parseObject(message);
            String token = jsonObject.getString("token");
            SysUser user = userMapper.getUserByToken(token);
            if (user!=null){
                if (user.getStatus()==1){
                    String location = jsonObject.getString("location");
                    locationMap.put(user.getMaintainerId(), location);
                    locationUserMap.put(session.getRequestParameterMap().get("sid").get(0),user.getMaintainerId());
                    redisTemplate.opsForValue().set("maintainerLocation",locationMap);
                    //redisTemplate.opsForValue().set(user.getMaintainerId(), location,10, TimeUnit.SECONDS);
                }
            }

        }
    }



    /**
     * 客户查看运维人员位置
     * @param message
     * @param session
     */
    @Override
    public void getMaintainerLocation(String message, Session session) {
        if (StringUtils.isNotBlank(message)){
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String maintainerId = jsonObject.getString("maintainerId");
            String projectId = jsonObject.getString("projectId");
            customerMap.put(sid,maintainerId+";"+projectId);
            Project project = projectMapper.queryProjectByProId(projectId);
            for (Map.Entry entry : locationMap.entrySet()) {
                if (maintainerId.equals(entry.getKey().toString())){
                    Double number = CalculateDistanceUtils.getDistance(entry.getValue().toString(), project.getLongitudeLatitude() + "," + project.getLatitude_latitude(), Ellipsoid.WGS84);
                    webSocketServer.sendInfo(sid,entry.getValue().toString() + ";" + project.getLongitudeLatitude() + "," + project.getLatitude_latitude()+";"+number);
                }
            }
        }

    }

    /**
     * 线缆测温
     * @param message
     * @param session
     */
    @Override
    public void sendTemperatureData(String message, Session session) {
        if (StringUtils.isNotBlank(message)){
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String deviceId = jsonObject.getString("deviceId");
            temperatureDataMap.put(sid,deviceId);
            Object o = redisTemplate.opsForValue().get(deviceId);
            webSocketServer.sendInfo(sid,JSONObject.toJSONString(o));
        }
    }

    /*
     * 电压设备数据
     * */
    @Override
    public void getVoltage(String message, Session session) {
        if (StringUtils.isNotBlank(message)){
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String powerRoomId = jsonObject.getString("powerRoomId");
            voltage.put(sid,powerRoomId);
            List<Voltage>  temperatureDeviceList=voltageMapper.queryVoltageList(powerRoomId);
            List<Voltage> temperatureDataList=new ArrayList<>();
            if (temperatureDeviceList.size()>0){
                ObjectMapper objectMapper=new ObjectMapper();
                for (Voltage temperatureDevice:temperatureDeviceList){
                    Object object = redisTemplate.opsForValue().get(temperatureDevice.getVoltageId());
                    if (object!=null){
                        Voltage temperatureData = objectMapper.convertValue(object, Voltage.class);
                        temperatureData.setAlarmStatus(1);
                        temperatureDataList.add(temperatureData);
                    }
                }
            }
            webSocketServer.sendInfo(sid,JSONObject.toJSONString(temperatureDataList));
        }
    }

    /**
     * 定时发送电压设备列表
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void sendVoltageList() {
        if (voltage.size()>0){
            for (Map.Entry  deviceMap: voltage.entrySet()) {
                String powerRoomId = deviceMap.getValue().toString();
                List<Voltage>  voltagenList=voltageMapper.queryVoltageList(powerRoomId);
                List<Voltage> temperatureDataList=new ArrayList<>();
                if (voltagenList.size()>0){
                    ObjectMapper objectMapper=new ObjectMapper();
                    for (Voltage temperatureDevice:voltagenList){
                        Object object = redisTemplate.opsForValue().get(temperatureDevice.getVoltageId());
                        if (object!=null){
                            Voltage temperatureData = objectMapper.convertValue(object, Voltage.class);
                            temperatureData.setAlarmStatus(1);
                            temperatureDataList.add(temperatureData);
                        }
                    }
                }
                webSocketServer.sendInfo(deviceMap.getKey().toString(),JSONObject.toJSONString(temperatureDataList));
            }
        }
    }


    /*
     * 变压器温控数据
     * */
    @Override
    public void getTempData(String message, Session session) {
        if (StringUtils.isNotBlank(message)){
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String powerRoomId = jsonObject.getString("powerRoomId");
            tempDataMap.put(sid,powerRoomId);

            TempDeviceExample example = new TempDeviceExample();
            TempDeviceExample.Criteria criteria = example.createCriteria();
            //todo powerroomId 改为string?
            criteria.andPowerroomIdEqualTo(Integer.valueOf(powerRoomId));
            List<TempDevice> tempDeviceList = tempDeviceMapper.selectByExample(example);

            List<TempData> tempDataList=new ArrayList<>();
            if (tempDeviceList.size() > 0) {
                for (TempDevice tempDevice:tempDeviceList){
                    Object object = redisTemplate.opsForValue().get(tempDevice.getDeviceId());
                    if (object!=null){
                        TempData tempData = JSONObject.parseObject(object.toString(),TempData.class);
                        tempDataList.add(tempData);
                    }
                }
            }
            webSocketServer.sendInfo(sid,JSONObject.toJSONString(tempDataList));
        }
    }

    /**
     * 定时发送变压器温控数据
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void sendTempDataList() {
        if (tempDataMap.size()>0){
            for (Map.Entry  deviceMap: tempDataMap.entrySet()) {
                String powerRoomId = deviceMap.getValue().toString();
                TempDeviceExample example = new TempDeviceExample();
                TempDeviceExample.Criteria criteria = example.createCriteria();
                criteria.andPowerroomIdEqualTo(Integer.valueOf(powerRoomId));
                List<TempDevice> tempDeviceList = tempDeviceMapper.selectByExample(example);
                List<TempData> tempDataList=new ArrayList<>();
                if (tempDeviceList.size()>0){
                    for (TempDevice tempDevice:tempDeviceList){
                        Object object = redisTemplate.opsForValue().get(tempDevice.getDeviceId());
                        if (object!=null){
                            TempData tempData = JSONObject.parseObject(object.toString(),TempData.class);
                            tempDataList.add(tempData);
                        }
                    }
                }
                webSocketServer.sendInfo(deviceMap.getKey().toString(),JSONObject.toJSONString(tempDataList));
            }
        }
    }

    /**
     * 综保数据
     * @param message
     * @param session
     */
    @Override
    public void getProtectData(String message, Session session) {
        if (StringUtils.isNotBlank(message)) {
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String deviceId = jsonObject.getString("deviceId");
            if (deviceId != null) {
                protectDataMap.put(sid, deviceId);
                Object protectData = redisTemplate.opsForValue().get(deviceId);
                webSocketServer.sendInfo(sid, JSONObject.toJSONString(protectData));
            } else {
                webSocketServer.sendInfo(sid, null);
            }
        }
    }

    /**
     * 定时发送综保数据
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void sendProtectData() {
        if (protectDataMap.size() > 0) {
            for (Map.Entry entry : protectDataMap.entrySet()) {
                Object protectData = redisTemplate.opsForValue().get(entry.getValue());
                webSocketServer.sendInfo(entry.getKey().toString(), JSONObject.toJSONString(protectData));
            }
        }
    }



        /*
        * 水浸设备数据
        * */
        @Override
        public void getWaterInvasion(String message, Session session) {
            if (StringUtils.isNotBlank(message)){
                String sid = session.getRequestParameterMap().get("sid").get(0);
                JSONObject jsonObject = JSONObject.parseObject(message);
                String powerRoomId = jsonObject.getString("powerRoomId");
                waterInvasion.put(sid,powerRoomId);
                List<WaterInvasion>  temperatureDeviceList=waterInvasionMapper.queryWaterInvasionList(powerRoomId);
                List<WaterInvasion> temperatureDataList=new ArrayList<>();
                if (temperatureDeviceList.size()>0){
                    ObjectMapper objectMapper=new ObjectMapper();
                    for (WaterInvasion temperatureDevice:temperatureDeviceList){
                        Object object = redisTemplate.opsForValue().get(temperatureDevice.getWaterlId());
                        if (object!=null){
                            WaterInvasion temperatureData = objectMapper.convertValue(object, WaterInvasion.class);
                            if(temperatureData.getWaterlInvasion() < 700){
                                temperatureData.setStatus(0);
                                temperatureData.setAlarmStatus(1);
                            }
                            temperatureData.setStatus(1);
                            temperatureDataList.add(temperatureData);
                        }
                    }
                }
                webSocketServer.sendInfo(sid,JSONObject.toJSONString(temperatureDataList));
            }
        }


    /**
     * 定时发送水浸设备列表
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void sendWaterInvasionList() {
        if (waterInvasion.size()>0){
            for (Map.Entry  deviceMap: waterInvasion.entrySet()) {
                String powerRoomId = deviceMap.getValue().toString();
                List<WaterInvasion>  waterInvasionList=waterInvasionMapper.queryWaterInvasionList(powerRoomId);
                List<WaterInvasion> temperatureDataList=new ArrayList<>();
                if (waterInvasionList.size()>0){
                    ObjectMapper objectMapper=new ObjectMapper();
                    for (WaterInvasion temperatureDevice:waterInvasionList){
                        Object object = redisTemplate.opsForValue().get(temperatureDevice.getWaterlId());
                        if (object!=null){
                            WaterInvasion temperatureData = objectMapper.convertValue(object, WaterInvasion.class);
                            if(temperatureData.getWaterlInvasion() < 700){
                                temperatureData.setAlarmStatus(1);
                                temperatureData.setStatus(0);
                            }
                            temperatureData.setStatus(1);
                            temperatureDataList.add(temperatureData);
                        }
                    }
                }
                webSocketServer.sendInfo(deviceMap.getKey().toString(),JSONObject.toJSONString(temperatureDataList));
            }
        }
    }



    /**
     * 线缆测温列表
     * @param message
     * @param session
     */
    @Override
    public void getTemperatureDeviceList(String message, Session session) {
        if (StringUtils.isNotBlank(message)){
            String sid = session.getRequestParameterMap().get("sid").get(0);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String powerRoomId = jsonObject.getString("powerRoomId");
            temperatureDeviceMap.put(sid,powerRoomId);
            List<TemperatureDevice>  temperatureDeviceList=temperatureDeviceMapper.getTemperatureDeviceByRoomId(powerRoomId);
            List<TemperatureData> temperatureDataList=new ArrayList<>();
            if (temperatureDeviceList.size()>0){
                ObjectMapper objectMapper=new ObjectMapper();
                for (TemperatureDevice temperatureDevice:temperatureDeviceList){
                    Object object = redisTemplate.opsForValue().get(temperatureDevice.getSerialNumber());
                    if (object!=null){
                        TemperatureData temperatureData = objectMapper.convertValue(object, TemperatureData.class);
                        temperatureDataList.add(temperatureData);
                    }
                }
            }
            webSocketServer.sendInfo(sid,JSONObject.toJSONString(temperatureDataList));
        }
    }


    /**
     * 定时发送线缆测温数据
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void sendTemperatureData() {
        if (temperatureDataMap.size()>0){
            for (Map.Entry  temperatureData: temperatureDataMap.entrySet()) {
                Object o = redisTemplate.opsForValue().get(temperatureData.getValue().toString());
                webSocketServer.sendInfo(temperatureData.getKey().toString(),JSONObject.toJSONString(o) );
            }
        }
    }

    /**
     * 定时发送线缆测温列表
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void sendTemperatureDeviceList() {
        if (temperatureDeviceMap.size()>0){
            for (Map.Entry  deviceMap: temperatureDeviceMap.entrySet()) {
                String powerRoomId = deviceMap.getValue().toString();
                List<TemperatureDevice>  temperatureDeviceList=temperatureDeviceMapper.getTemperatureDeviceByRoomId(powerRoomId);
                List<TemperatureData> temperatureDataList=new ArrayList<>();
                if (temperatureDeviceList.size()>0){
                    ObjectMapper objectMapper=new ObjectMapper();
                    for (TemperatureDevice temperatureDevice:temperatureDeviceList){
                        Object object = redisTemplate.opsForValue().get(temperatureDevice.getSerialNumber());
                        if (object!=null){
                            TemperatureData temperatureData = objectMapper.convertValue(object, TemperatureData.class);
                            temperatureDataList.add(temperatureData);
                        }
                    }
                }
                webSocketServer.sendInfo(deviceMap.getKey().toString(),JSONObject.toJSONString(temperatureDataList));
            }
        }
    }



    /**
     * 向客户定时发送运维人员位置
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void sendMaintainerLocation() {
        if (locationMap.size()>0 && customerMap.size()>0){
            for (Map.Entry locationMap : locationMap.entrySet()) {
                for (Map.Entry customerMap : customerMap.entrySet()) {
                    String[] strArr = customerMap.getValue().toString().split(";");
                    if (locationMap.getKey().toString().equals(strArr[0])){
                        Project project = projectMapper.queryProjectByProId(strArr[1]);
                        Double number = CalculateDistanceUtils.getDistance(locationMap.getValue().toString(), project.getLongitudeLatitude() + "," + project.getLatitude_latitude(), Ellipsoid.WGS84);
                        webSocketServer.sendInfo(customerMap.getKey().toString(),locationMap.getValue().toString()+ ";" + project.getLongitudeLatitude() + "," + project.getLatitude_latitude()+";"+number );
                    }

                }
            }
        }

    }

    /**
     * 发送电路图实时数据
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void powerImg() {
       /* DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info(sdf.format(new Date())+"*********每6秒执行一次");*/
        if (powerImgMap.size() > 0) {
            for (Map.Entry entry : powerImgMap.entrySet()) {
                String str = entry.getValue().toString();
                List<Device> deviceList = JSONObject.parseArray(str, Device.class);
                List list = new ArrayList<>();
                for (Device device : deviceList) {
                    list.add(redisTemplate.opsForValue().get(device.getSerialNumber()));
                }
                webSocketServer.sendInfo(entry.getKey().toString(), JSONObject.toJSONString(list));
            }
        }
    }

    /**
     * 发送电路图电器火灾设备实时数据
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void powerImgTemperatureData() {
        if (powerImgTemperatureDeviceMap.size() > 0) {
            for (Map.Entry entry : powerImgTemperatureDeviceMap.entrySet()) {
                String str = entry.getValue().toString();
                List<TemperatureDevice> temperatureDeviceList = JSONObject.parseArray(str, TemperatureDevice.class);
                List list = new ArrayList<>();
                for (TemperatureDevice temperatureDevice : temperatureDeviceList) {
                    list.add(redisTemplate.opsForValue().get(temperatureDevice.getSerialNumber()));
                }
                webSocketServer.sendInfo(entry.getKey().toString(), JSONObject.toJSONString(list));
            }
        }
    }


    /**
     * 发送实时数据
     */
    @Scheduled(cron = "0/6 * * * * ?")
    public void powerData() {
      /*  DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info(sdf.format(new Date())+"*********每6秒执行一次");*/
        if (powerDataMap.size() > 0) {
            for (Map.Entry entry : powerDataMap.entrySet()) {
                Object powerData = redisTemplate.opsForValue().get(entry.getValue());
                webSocketServer.sendInfo(entry.getKey().toString(), JSONObject.toJSONString(powerData));
            }
        }
    }




    /**
     * 车辆实时位置、首页项目坐标跳动
     */
    @Scheduled(cron = "0/3 * * * * ?")
    public void senCarLocation() {
      /*  DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info(sdf.format(new Date())+"*********每3秒执行一次");*/
        List<Distance> list = new ArrayList<>();
        if (locationMap.size() > 0) {
            for (Map.Entry entry : locationMap.entrySet()) {
                SysUser user = userMapper.getUserByMaintainerId(Integer.valueOf(entry.getKey().toString()));
                if (user != null) {
                    WorkOrder workOrder = workOrderMapper.getWorkOrderByMaintainerId(user.getMaintainerId());
                    if (workOrder != null) {
                        Project project = projectMapper.queryProjectByProId(workOrder.getProjectId());
                        Double number = CalculateDistanceUtils.getDistance(entry.getValue().toString(), project.getLongitudeLatitude() + "," + project.getLatitude_latitude(), Ellipsoid.WGS84);
                        Distance distance = new Distance();
                        distance.setSource(entry.getValue().toString());
                        distance.setDistance(number);
                        distance.setTarget(project.getLongitudeLatitude() + "," + project.getLatitude_latitude());
                        distance.setProjectId(project.getProjectId());
                        distance.setName(user.getUsername());
                        list.add(distance);
                    } else {
                        Distance distance = new Distance();
                        distance.setSource(entry.getValue().toString());
                        distance.setName(user.getUsername());
                        list.add(distance);
                    }
                }
            }
            String s = JSONObject.toJSONString(list);
            for (Map.Entry entry : projectAlarmStatusMap.entrySet()) {
                webSocketServer.sendInfo(entry.getKey().toString(), "{\"data\":" + s + ",\"method\":\"carLocation\"}");
            }
        }
        if (projectAlarmStatusMap.size()>0){
            List<Project> projectList=new ArrayList<>();
            List<Project> projects = projectMapper.queryProjectAll();
            for (Project project:projects){
                OrderDelivery orderDelivery = orderDeliveryMapper.getAlarmStatusByProjectId(project.getProjectId());
                if (orderDelivery!=null){
                    projectList.add(project);
                }
            }
            String s = JSONObject.toJSONString(projectList);
            for (Map.Entry entry : projectAlarmStatusMap.entrySet()) {
                webSocketServer.sendInfo(entry.getKey().toString(), "{\"data\":" + s + ",\"method\":\"projectAlarmStatus\"}");
            }
        }
    }


}
